<html>
<head>

<title>Groovy Goodness: Using AntBuilder</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Using AntBuilder</h3>

<div class="post">
<p>Groovy 1.6.5 includes Ant 1.7.1 and we can use Ant from our Groovy code with the <a href="http://groovy.codehaus.org/gapi/groovy/util/AntBuilder.html">AntBuilder</a> class. The AntBuilder provides a nice DSL to create Ant tasks and to execute them. The following piece of code shows some samples of what we can do with the AntBuilder:</p>
<pre class="brush:groovy">
// Create AntBuilder.
def ant = new AntBuilder()

// Simple echo task.
def world = 'Groovy'
ant.echo("Hello $world")  // Output: [echo] Hello Groovy

// Ant task properties are defined as a map.
def options = [src: 'http://mrhaki.blogspot.com', dest: 'blog.html']
ant.get(options)
// is the same as
ant.get(src: 'http://mrhaki.blogspot.com', dest: 'blog.html']

// Nice builder syntax for tasks with for example filesets.
def zipfile = 'test.zip'
def current = '.'
ant.zip(destfile: zipfile) {
    fileset(dir: current) {
        include(name: '**/*.txt')
    }
}
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>